<?php
require_once ("db/AbstractDb.php");
require_once ("model/Category.php");

/**
 * Jelikoz tady nebudou interfacy, tak to bude rovnou naimplementovano,
 * kazdopadne, je moznost pak zmenit DAO tridy napr. pro jinou db aniz by
 * to melo vliv na kod ve vyssi vrstve (controllerech).. -> to neni bohuzel pravda
 * bez Factory ktera by nezavisle vytvarela objekty a pracovalo se s nima jako s interfacem..
 * ..nevadi no..
 * 
 * poznamka - vsechny DAO tridy by mely dedit z AbstractDb pro snadny pristup
 * k db (query)
 */
class CategoryDAO extends AbstractDb {
 
	/**
	 * Vstupnim parametrem je pole z funkce mysql_fetch_array
	 * Vraci objekt category
	 */
	private function createCategoryInternal($pole){
		
		$result = new Category();
		
			$result->setCategoryId($pole["cat_id"]);
			$result->setCategoryName($pole["cat_name"]);
			$result->setCategoryHref($pole["cat_href"]);
			$result->setCategoryParent($pole["cat_parent"]);
		
		return $result;
	}


	public function loadCategory($id) {

		$result = $this->query("SELECT * FROM category WHERE cat_id = " . $id);

		$category = new Category();

		//zpracovani resultu a predelani do objektu..
		while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {

			$category->setCategoryId($id);

			//tady to jde udelat i cislem, ale tim ze sem nastavil
			//na both, tak tam muze byt identifikace cislem
			//i nazvem z tabulky - jmenem to sice
			//vyzaduje znat presne nazvy atributu v db

			$category->setCategoryName($row["cat_name"]);
			$category->setCategoryHref($row["cat_href"]);
			$category->setCategoryParent($row["cat_parent"]);
		}

		return $category;
	}
	
	/**
	 * Nahraje z databaze list kategorii(subkategorii) podle zadaneho cat_parent
	 */
	
	/**
	 * Vraci array objektu Category
	 */
	public function loadCategoryByParent($parent){
		
		$queryString = "SELECT * FROM category WHERE cat_parent =".$parent;
		
		if($parent == null){
			$queryString = "SELECT * FROM category WHERE cat_parent IS NULL";
		}
		
		$result = $this->query($queryString);
		//echo "proveden dotaz ".$queryString;
		
		$return = array();
		
		while($row = mysql_fetch_array($result)) {
       		$return[] = $this->createCategoryInternal($row);
   		}
   
   			return $return;
	}
	
	
	/**
	 * Vstupnim parametrem je instance tridy Category
	 */
	public function createCategory($category) {

		$sql_query = "INSERT INTO category (cat_id, cat_name, cat_href, cat_parent) " .
		"VALUES (" . $category->getCategoryId() . ", " . $category->getCategoryName() . ", " . $category->getCategoryHref() . ", " . $category->getCategoryParent() . ")";
 
		//dodelat..

	}
	/**
	 * Vstupnim parametrem je instance tridy Category
	 */
	public function updateCategory($category) {

	}

	/**
	 * Vstupnim parametrem je ID kategorie
	 */
	public function deleteCategory($id) {

	}

}
?>
